[BugFix][TOPI] Fix resize accuracy with non-floor rounding#18835
Conversation
`can_convert_multiply_to_intdiv` was enabled unconditionally for `nearest_neighbor` + `asymmetric` mode, even when `rounding_method` is not `floor`. This converted the mapped coordinate via integer division (equivalent to floor) regardless of the requested rounding, producing wrong results whenever the target size is an exact integer multiple of the source size. Fix: guard the `can_convert_multiply_to_intdiv` optimisation so it only activates when `rounding_method` is `"floor"` or `""` (which defaults to `"floor"` for asymmetric mode). Also update the Python reference implementation (`resize_python.py`) to accept and thread a `rounding_method` parameter through all public entry points (`get_index`, `resize3d_nearest`, `resize3d_ncdhw`, `resize1d_python`, `resize2d_python`, `resize3d_python`), so the reference matches the corrected TVM behaviour. Add `tests/python/te/test_topi_resize2d.py` with regression tests that exercise the buggy path (non-floor rounding + asymmetric + integer scale factor) as well as sanity checks for floor rounding and non-integer scale factors. Fixes: apache#16137
Summary of ChangesHello @tqchen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves an accuracy issue in the image resize operation, specifically for the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request fixes a bug in the resize operator where an optimization was incorrectly applied for non-floor rounding methods. The fix correctly guards this optimization. The PR also updates the Python reference implementation to support different rounding methods. My review found some issues in the updated reference implementation in resize_python.py, where the rounding logic did not correctly match the TVM operator for all cases, and it handled unknown rounding methods differently. I've provided a suggestion to fix these issues in the test helper function to ensure its correctness and consistency with the main operator.
| def get_index(x, image_width, target_width, coordinate_transformation_mode, rounding_method=""): | ||
| """get and round the nearest index for nearest_neighbor""" | ||
| in_x = get_inx(x, image_width, target_width, coordinate_transformation_mode) | ||
| if coordinate_transformation_mode == "align_corners": | ||
| # round prefer ceil | ||
| if rounding_method == "" or rounding_method == "floor": | ||
| if coordinate_transformation_mode == "align_corners": | ||
| out = math.floor(in_x + 0.5) | ||
| else: | ||
| out = math.floor(in_x) | ||
| elif rounding_method == "round": | ||
| out = math.floor(in_x + 0.5) | ||
| elif rounding_method == "round_prefer_floor": | ||
| out = math.ceil(in_x - 0.5) | ||
| elif rounding_method == "round_prefer_ceil": | ||
| out = math.floor(in_x + 0.5) | ||
| elif rounding_method == "ceil": | ||
| out = math.ceil(in_x) | ||
| else: | ||
| out = math.floor(in_x) | ||
| out = max(min(out, image_width - 1), 0) | ||
| return out |
There was a problem hiding this comment.
The implementation of get_index has a few issues that could lead to incorrect test results or mask bugs:
- Incorrect Rounding for
"round"method: The logic forrounding_method="round"usesmath.floor(in_x + 0.5), which is "round half up". The TVM operatorte.roundimplements "round half to even". The reference implementation should use Python's built-inround()to match this behavior. This also affects the default behavior forcoordinate_transformation_mode="align_corners". - Incorrect logic for
"floor"method: Whenrounding_method="floor"andcoordinate_transformation_mode="align_corners", the current code applies "round half up" instead of flooring. The TVM operator does not alter the rounding method if it's explicitly provided. - Inconsistent Error Handling: Unknown rounding methods silently fall back to
floor. This differs from the main operator, which raises aValueError. The test helper should also raise an error to detect invalid inputs. - Return Type: The function returns a
floatfor an index, which is less precise. It should return anint.
I suggest refactoring the function to align with the TVM operator's logic, which will fix these issues and improve clarity.
def get_index(x, image_width, target_width, coordinate_transformation_mode, rounding_method=""):
"""get and round the nearest index for nearest_neighbor"""
in_x = get_inx(x, image_width, target_width, coordinate_transformation_mode)
effective_rounding_method = rounding_method
if not effective_rounding_method:
if coordinate_transformation_mode == "align_corners":
effective_rounding_method = "round"
else:
effective_rounding_method = "floor"
if effective_rounding_method == "floor":
out = math.floor(in_x)
elif effective_rounding_method == "round":
# Use python's built-in round to match te.round (round half to even)
out = round(in_x)
elif effective_rounding_method == "round_prefer_floor":
out = math.ceil(in_x - 0.5)
elif effective_rounding_method == "round_prefer_ceil":
out = math.floor(in_x + 0.5)
elif effective_rounding_method == "ceil":
out = math.ceil(in_x)
else:
raise ValueError(f"Unknown rounding method: {rounding_method!r}")
out = max(min(out, image_width - 1), 0)
return int(out)
Fixes a resize accuracy bug where
can_convert_multiply_to_intdivoptimization was applied unconditionally fornearest_neighbor+asymmetricmode, even when the rounding method is notfloor. This caused incorrect output forround/ceilrounding when target dimensions are integer multiples of source dimensions.The fix guards the optimization to only apply when
rounding_methodisflooror empty (default).Also updates the reference implementation in
resize_python.pyto properly handle non-floor rounding methods.Based on #16137.